package com.fqyd.campustransaction.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fqyd.campustransaction.model.Appraise;
import com.fqyd.campustransaction.model.Goods;
import com.fqyd.campustransaction.model.Order;
import com.fqyd.campustransaction.model.OrderDetails;
import com.fqyd.campustransaction.model.User;
import com.fqyd.campustransaction.model.entity.OrderDetailsItem;
import com.fqyd.campustransaction.service.AppraiseService;
import com.fqyd.campustransaction.service.GoodsService;
import com.fqyd.campustransaction.service.OrderDetailsService;
import com.fqyd.campustransaction.service.OrderService;
import com.fqyd.campustransaction.service.UserService;
import com.fqyd.campustransaction.utils.Tools;

/**
 * 评价管理
 * 
 * @author lixiaochong
 *
 * @create 2019年4月22日
 * 
 */
@Controller
public class AppraiseController extends BaseController {

	@Autowired
	private AppraiseService appraiseService;
	
	@RequestMapping("personal_appraise")
	public String index(HttpServletRequest request, String orderId) {
		User user = (User) request.getSession().getAttribute("loguser");
		user = userService.getById(user.getId());
		request.setAttribute("user", user);
		request.setAttribute("orderId", orderId);
		return "personal_appraise";
	}
	
	@RequestMapping("add_appraise")
	public String addAppraise(HttpServletRequest request, String orderId, Integer star_count, String content) {
		try {
			User user = (User) request.getSession().getAttribute("loguser");
			Appraise appraise = new Appraise();
			appraise.setId(Tools.createID());
			appraise.setOrderId(orderId);
			appraise.setStarCount(star_count);
			appraise.setUserId(user.getId());
			appraise.setContent(content);
			appraise.setCreateTime(new Date());
			List<OrderDetails> orderDetails = orderDetailsService.getByOrderId(orderId);
			if(orderDetails != null && orderDetails.size() > 0){
				OrderDetails details = orderDetails.get(0);
				Goods goods = goodsService.getById(details.getOtherId());
				appraise.setSaleId(goods.getUserId());
			}
			appraiseService.add(appraise);
			User saleUser = userService.getById(appraise.getSaleId());
			if(saleUser.getPower() == 0){
				saleUser.setPower(star_count);
			}else {
				saleUser.setPower((saleUser.getPower() + star_count)/2);
			}
			userService.update(saleUser);
			Order order = orderService.getById(orderId);
			order.setStatus("已评价");
			orderService.update(order);
			List<Order> orders = orderService.getByUserId(user.getId());
			List<OrderDetailsItem> orderDetailsItems = new ArrayList<>();
			for (int i = 0; i < orders.size(); i++) {
				List<OrderDetails> orderDetailss = orderDetailsService.getByOrderId(orders.get(i).getId());
				OrderDetailsItem detailsGoods = new OrderDetailsItem();
				detailsGoods.setOrder(orders.get(i));
				detailsGoods.setOrderDetails(orderDetailss);
				orderDetailsItems.add(detailsGoods);
			}
			request.setAttribute("orderDetailsItems", orderDetailsItems);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "personal_order";
	}
	
}
